package com.kedsoft.andaily.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.kedsoft.andaily.common.CommonResult;
import com.kedsoft.andaily.entity.Boardroom;
import com.kedsoft.andaily.entity.Cunsumer;
import com.kedsoft.andaily.entity.Normal;
import com.kedsoft.andaily.entity.Task;
import com.kedsoft.andaily.service.BoardroomService;
import com.kedsoft.andaily.service.CunsumerService;
import com.kedsoft.andaily.service.NormalService;
import com.kedsoft.andaily.service.TaskService;
import com.kedsoft.andaily.vo.PageVo;
import com.kedsoft.andaily.vo.Pagination;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/******************************************************************
 * @文件名称 : BoardRoomApiController
 * @文件作者 : 卢素强
 * @创建时间 : 2019/4/25 11:48
 * @文件描述 : 
 * @修改历史 : 2019/4/25
 ******************************************************************/
@Api("配电房管理")
@RestController
@RequestMapping(value = "/api/boardroom")
public class BoardRoomApiController {
    @Autowired
    BoardroomService boardroomService;

    @Autowired
    CunsumerService cunsumerService;

    @Autowired
    TaskService taskService;

    @Autowired
    NormalService normalService;

    @ApiOperation(value = "我的配电房列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "page", dataType = "int",defaultValue = "1", required = true, value = "第几页"),
            @ApiImplicitParam(paramType = "query", name = "rows", dataType = "int",defaultValue = "20", required = true, value = "行数"),
            @ApiImplicitParam(paramType = "query", name = "sort", dataType = "String", defaultValue = "id", required = false, value = "排序主键"),
            @ApiImplicitParam(paramType = "query", name = "order", defaultValue = "asc", dataType = "String", required = false, value = "排序"),
    })
    @RequestMapping(value = "pageList", method = RequestMethod.GET)
    public CommonResult<PageVo<Boardroom>> getPageList(@CookieValue("userName") String userName, Pagination pagination){
        int routerid = 0;
        Cunsumer cunsumer = cunsumerService.getCunsumerByMobile(userName);
        if(cunsumer != null)
        {
            routerid = cunsumer.getRouterid();
        }
        EntityWrapper<Boardroom> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("routerid", routerid);

        if(pagination.getOrder()!=null) {
            if (pagination.getOrder().toLowerCase().equals("asc")){
                entityWrapper.orderBy(pagination.getSort(), true);
            }else{
                entityWrapper.orderBy(pagination.getSort(), false);
            }
        }
        Page<Boardroom> page = new Page<>(pagination.getPage()==null?1:pagination.getPage(),
                pagination.getRows()==null?20:pagination.getRows());

        boardroomService.selectPage(page,entityWrapper);
        PageVo<Boardroom> pageVo = new PageVo<>(page.getRecords(),page.getTotal());
        return CommonResult.success(pageVo);
    }

    @ApiOperation(value = "我的配电房列表")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public CommonResult<PageVo<Boardroom>> getList(@CookieValue("userName") String userName){
        int routerid = 0;
        Cunsumer cunsumer = cunsumerService.getCunsumerByMobile(userName);
        if(cunsumer != null)
        {
            routerid = cunsumer.getRouterid();
        }
        EntityWrapper<Boardroom> entityWrapper = new EntityWrapper<>();
        if(cunsumer.getLeader() == 2)
        {
            //管理员，查看所有
        }
        else
        {
            entityWrapper.eq("routerid", routerid);
        }
        entityWrapper.orderBy("routerid", true);
        PageVo<Boardroom> pageVo = new PageVo<>(boardroomService.selectList(entityWrapper),boardroomService.selectCount(entityWrapper));
        return CommonResult.success(pageVo);
    }

    @ApiOperation(value = "修正位置")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", dataType = "int", required = true, value = "ID"),
            @ApiImplicitParam(paramType = "query", name = "lng", dataType = "double", required = true, value = "经度"),
            @ApiImplicitParam(paramType = "query", name = "lat", dataType = "double", required = true, value = "纬度")
    })
    @RequestMapping(value = "updatePos", method = RequestMethod.POST)
    public CommonResult<Boardroom> updatePos(@RequestParam("id") int id, @RequestParam("lng") double lng, @RequestParam("lat") double lat)
    {
        Boardroom boardroom = boardroomService.selectById(id);
        boardroom.setBoardlat(BigDecimal.valueOf(lat));
        boardroom.setBoardlng(BigDecimal.valueOf(lng));

        boardroomService.updateById(boardroom);

        //修改已经生成的任务经纬度
        EntityWrapper<Task> taskEntityWrapper = new EntityWrapper<>();
        taskEntityWrapper.eq("boardid",id);
        //已经接单的任务不能修改
        taskEntityWrapper.eq("status",10);
        Task taskBean = new Task();
        taskBean.setBoardlng(BigDecimal.valueOf(lng));
        taskBean.setBoardlat(BigDecimal.valueOf(lat));
        taskService.update(taskBean,taskEntityWrapper);

        //修改已经生成的任务经纬度
        EntityWrapper<Normal> normalEntityWrapper = new EntityWrapper<>();
        normalEntityWrapper.eq("boardid",id);
        Normal normalBean = new Normal();
        normalBean.setBoardlng(BigDecimal.valueOf(lng));
        normalBean.setBoardlat(BigDecimal.valueOf(lat));
        normalService.update(normalBean,normalEntityWrapper);

        return CommonResult.success(boardroom);
    }

}
